/**
 * 2017.4.28
 */
package cn.edu.bjtu.entity.impl.local;

import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.locks.ReadWriteLock;

import cn.edu.bjtu.alex.rewrite.Context;
import cn.edu.bjtu.alex.rewrite.TrainDriver;

/**
 * 建立tfidf模型,用来实现对于自然文本向量的转换
 
 * 如果是异步的话保留着命令执行的的状态及结果
 * 如果是同步的简单命令应该用不到
 * 
 * @author alex
 *
 */
public class TFIDFExecuteContext extends BaseTFIDFExecuteContext{
	Map<String,Object> resultMap = new TreeMap<String, Object>();
	private boolean nocache  = false;
	/**
	 * @param lock
	 * @param in
	 * @param out
	 * @param ctx
	 */
	public TFIDFExecuteContext(ReadWriteLock lock, String in, String out, Context ctx) {
		super(lock, in, out, ctx);
	}
	

	@Override
	public boolean isNoCache() {
		return nocache;
	}
	@Override
	public void fireInternal() throws Exception{		
		if(ctx.transLock.writeLock().tryLock()){
			try{
				if(isNoCache()){
					this.ctx.reset();
				}
				new TrainDriver(this.ctx).fit();
				this.msg = "TFIDF建立成功";
				resultMap.put("tip", "转换成功");
			}finally{
				ctx.transLock.writeLock().unlock();
			}
		}else{
			msg = "任务已经提交,正在后台执行,稍安勿躁.";
		}
	}
	/**
	 * @Param
	 * @Author zhangzhidong
	 */
	
	@Override
	public Map<String, Object> getResultMapForRender() {
		return resultMap;
	}

	@Override
	public Context javaClassResult() {
		return this.ctx;
	}
		
	


}
